<?php 

	//This PHP file will show all the registers that match with the selection that the user will do on the web page.
	if($_SERVER['REQUEST_METHOD']=='POST'){
		date_default_timezone_set('Europe/Madrid');
		$grade=$_POST['grd'];
		$subject=$_POST['sbj'];
		$teacher=$_POST['tchr'];

		$query= 'SELECT f.Code, f.Name, f.UserName FROM (Files f INNER JOIN Grade g on f.CodeGrade = g.Code) INNER JOIN Subject s on f.CodeSubject = s.Code';
		//index 0=f.Name and index 1=f.Code on mysql_fetch_array(variable, MYSQL_NUM)

		if (!HasLogged()){
			if ($grade == "" && $subject=="" && $teacher==""){
				$query .= ' where f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
			} else {
				if ($grade != ""){
					if ($subject =="" && $teacher == "") {
						$query .= ' where g.Name ="'.$grade.'" and f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
					}
					else{
						$query .= ' where g.Name ="'.$grade.'"';
					}
				}
				if ($subject != ""){
					if ($grade == ""){
						if ($teacher == ""){
							$query .= ' where s.Name ="'.$subject.'" and f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
						} else {
							$query .= ' where s.Name ="'.$subject.'"';
						}
					}
					else if ($grade != "" && $teacher == ""){
						$query .= ' and s.Name ="'.$subject.'" and f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
					} else if ($grade != "" && $teacher != ""){
						$query .= ' and s.Name ="'.$subject.'"';
					}
				}
				if ($teacher != ""){
					if ($grade == ""){
						if ($subject != "") 
							$query .= ' and f.UserName ="'.$teacher.'" and f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
						else 
							$query .= ' where f.UserName ="'.$teacher.'" and f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
					}
					else
						$query .= ' and f.UserName ="'.$teacher.'" and f.ActivationDate <= NOW() and f.DateExpiry >= NOW()';
				}
			}
		}
		elseif (HasLogged()){
			$query .= ' where f.UserName="'.$_SESSION["logID"].'"';
		}

		if ($r = mysql_query($query, $dbc)) { // Run the query.
		// Retrieve and print every record:
			echo '<div id="registers" class="registers">';
			if (!HasLogged()) {
				echo '<form id="checkPassword" method="GET" action="checkPassword.php">';	
			}
			while ($row = mysql_fetch_array($r, MYSQL_NUM)) { // Takes the information and creates an associative array of it 
				if (HasLogged()){
					echo '<div class="registers-box"><div class="registers-names">'.stripslashes($row[1])."</div>
					<div class='registers-links'><a href=\"edit.php?id={$row[0]}\">Editar</a>
					<a href=\"delete.php?id={$row[0]}\"'>Eliminar</a></div></div>";
				} else {
					$code=$row[0];
					$name=$row[1];
					echo '<div class="registers-box"><div class="registers-names">'.stripslashes($row[1]).'</div>
						<div class="registers-links"><input type="hidden" name="code" value="'.$row[0].'" />
						<input type="submit" class="button" value="Descarrega" /></div></div>';
				}
			}
			if (!HasLogged()) {
				echo '</form>';	
			}
			echo '</div>';
		} else { // Query didn't run.
			print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
		} // End of query IF.
	}

?>
	
